/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SuperChatUsernamePrompt.java
 *
 * Created on 27 sept. 2011, 09:02:33
 */
package superchat;

import javax.swing.JDialog;
/**
 * Classe de fenêtre de login
 * @author georgesm
 */
public class SuperChatNickPrompt extends javax.swing.JDialog {

    /** Creates new form SuperChatUsernamePrompt */
    public SuperChatNickPrompt(java.awt.Frame parent, boolean modal, Param param, SuperChatView mainView) {
        super(parent, modal);
        initComponents();
        this.mainView = mainView;
        this.param = param;
        Edit_pseudo.setText(param.nickname);
        Edit_pseudo2.setText(param.nickname);
        this.setTitle("Se connecter");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        Edit_pseudo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        Edit_ip = new javax.swing.JTextField();
        Button_connect = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        Edit_port = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        Edit_pseudo2 = new javax.swing.JTextField();
        Button_host = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        Edit_port2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });
        jTabbedPane1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTabbedPane1PropertyChange(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(superchat.SuperChatApp.class).getContext().getResourceMap(SuperChatNickPrompt.class);
        Edit_pseudo.setText(resourceMap.getString("nickname.text")); // NOI18N
        Edit_pseudo.setName("nickname"); // NOI18N
        Edit_pseudo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Edit_pseudoActionPerformed(evt);
            }
        });
        Edit_pseudo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Edit_pseudoKeyReleased(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        Edit_ip.setText(resourceMap.getString("Edit_ip.text")); // NOI18N
        Edit_ip.setName("Edit_ip"); // NOI18N
        Edit_ip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Edit_ipActionPerformed(evt);
            }
        });

        Button_connect.setText(resourceMap.getString("Button_connect.text")); // NOI18N
        Button_connect.setName("Button_connect"); // NOI18N
        Button_connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_connectActionPerformed(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        Edit_port.setText(resourceMap.getString("Edit_port.text")); // NOI18N
        Edit_port.setName("Edit_port"); // NOI18N
        Edit_port.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Edit_portKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Edit_pseudo, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(Button_connect, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Edit_ip, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Edit_port, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Edit_pseudo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(Edit_ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Edit_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Button_connect)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Se connecter", jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        Edit_pseudo2.setName("Edit_pseudo2"); // NOI18N
        Edit_pseudo2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Edit_pseudo2KeyReleased(evt);
            }
        });

        Button_host.setText(resourceMap.getString("Button_host.text")); // NOI18N
        Button_host.setName("Button_host"); // NOI18N
        Button_host.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_hostActionPerformed(evt);
            }
        });

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        Edit_port2.setText(resourceMap.getString("Edit_port2.text")); // NOI18N
        Edit_port2.setName("Edit_port2"); // NOI18N
        Edit_port2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Edit_port2ActionPerformed(evt);
            }
        });
        Edit_port2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Edit_port2KeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Edit_pseudo2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                    .addComponent(Button_host, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(Edit_port2, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addGap(12, 12, 12)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Edit_pseudo2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(Button_host)
                .addContainerGap())
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(78, 78, 78)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(Edit_port2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(60, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab("Héberger", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTabbedPane1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1PropertyChange
        // TODO add your handling code here:
}//GEN-LAST:event_jTabbedPane1PropertyChange

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
        // TODO add your handling code here:
}//GEN-LAST:event_jTabbedPane1StateChanged

    private void Button_hostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button_hostActionPerformed
        // TODO add your handling code here:
        if(Edit_pseudo2.getText().equals("") || Edit_port2.getText().equals("")) {
            mainView.alert("Vous avez oublié un paramètre.");
        } else if(param.serveur == null) {
            param.nickname = Edit_pseudo2.getText();
            param.port = Integer.valueOf(Edit_port2.getText());
            param.serveur = new Serveur(param.port);
            param.serveur.start();
            
            param.nickname = Edit_pseudo2.getText();
            param.ipAdress = "localhost";
            param.port = Integer.parseInt(Edit_port2.getText());
            mainView.connexion(param.port, param.nickname, param.ipAdress);
        }
        else {
            mainView.alert("Le serveur est déjà lancé.");
        }
}//GEN-LAST:event_Button_hostActionPerformed

    private void Edit_pseudo2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Edit_pseudo2KeyReleased
        // TODO add your handling code here:
        Edit_pseudo.setText(Edit_pseudo2.getText());
}//GEN-LAST:event_Edit_pseudo2KeyReleased

    private void Edit_pseudoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Edit_pseudoKeyReleased
        // TODO add your handling code here:
        Edit_pseudo2.setText(Edit_pseudo.getText());
}//GEN-LAST:event_Edit_pseudoKeyReleased

    private void Edit_port2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Edit_port2KeyReleased
        // TODO add your handling code here:
        Edit_port.setText(Edit_port2.getText());
    }//GEN-LAST:event_Edit_port2KeyReleased

    private void Edit_portKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Edit_portKeyReleased
        // TODO add your handling code here:
        Edit_port2.setText(Edit_port.getText());
    }//GEN-LAST:event_Edit_portKeyReleased

    private void Button_connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button_connectActionPerformed
        // TODO add your handling code here:
       if(Edit_pseudo.getText().equals("") || Edit_port.getText().equals("")|| Edit_ip.getText().equals("")) {
            mainView.alert("Vous avez oublié un paramètre.");
        } else {
            param.nickname = Edit_pseudo.getText();
            param.ipAdress = Edit_ip.getText();
            param.port = Integer.parseInt(Edit_port.getText());
            mainView.connexion(param.port, param.nickname, param.ipAdress);
        }    
    }//GEN-LAST:event_Button_connectActionPerformed

    private void Edit_pseudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Edit_pseudoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Edit_pseudoActionPerformed

    private void Edit_port2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Edit_port2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Edit_port2ActionPerformed

    private void Edit_ipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Edit_ipActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Edit_ipActionPerformed

    SuperChatView mainView;
    Param param;
    
    private JDialog alertBox;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Button_connect;
    private javax.swing.JButton Button_host;
    private javax.swing.JTextField Edit_ip;
    private javax.swing.JTextField Edit_port;
    private javax.swing.JTextField Edit_port2;
    private javax.swing.JTextField Edit_pseudo;
    private javax.swing.JTextField Edit_pseudo2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}
